var id;
var groupid;
var iconid;
var subid;
var menuid;
var dictBasicType;

$(function(){
   	 groupid = ly.getUrlParam("groupid");
	 iconid = ly.getUrlParam("iconid");
	 subid = ly.getUrlParam("subid");
	 menuid = ly.getUrlParam("menuid");	

	dictBasicType = ly.getUrlParam("dictBasicType");
	id = ly.getUrlParam("id");
	if(dictBasicType == 2){
		$("#title").html("编辑");
		  $(".form").getForm({
	        id: id,ajaxform:true
	   },function (data) {
	    	$("#isUsing").getSelect({
				type:"radio",
				asName:"isUsing",
				list:[
						{value:"1",display:"启用"},
						{value:"2",display:"禁用"}
			    	],
				default:data.isUsing!=null&&data.isUsing==false?'1':'2'
			});
	    });
	}else{
		$("#isUsing").getSelect({
				type:"radio",
				asName:"isUsing",
				list:[
					{value:"1",display:"启用"},
					{value:"2",display:"禁用"}
			      ],
				default:'1'
			});
	}
	
	//获取到选择的值
	$("input[name=bannerUrl]").on("click",function(){
		console.log(this.value);
	});
	
	//提交
    $("body").on("click","#save-btn-commodityClassification",function(event){
        event.preventDefault();
        var success =  $('.form').validate('submitValidate');
        if(success){
            //验证通过提交表单
            submitFom();
        }
    })
	
	//批量删除
	$("#batchDelBtn").click(function(){
		var c = " " ;
		$("input[name=datachk]:checked").each(function(){
			c += $(this).val()+',';
		});
		deleteFirstCategory(c);
	});
	
	$("#code").on("change",function(){
		var code = $(this).val();
		if(code!= null && code !=''){
			request({
				url:site.apiUrl+"/dictBasic/selectDictBasicMAXfuncValue",
				data:{'code':code},
				type:"get",				
				callback:function(f) {
					console.log(f);
					if(f.code=="rest.success"){
						$("#value").val(f.result);
					}else{
						ly.toast(f.desc);
					}
				}
			});
		}
	});
	
});

/*
 * 提交表单
 */
function submitFom(){
    var code =$("#code").val();
 	var value =$("#value").val();
 	var display = $("#display").val();
 	var dictOrder = $("#dictOrder").val();
 	var isUsing = $("#isUsing input:checked").val();
 	
 	
  	var obj = {};
  	var url = site.apiUrl+"/dictBasic/insertOrUpdateDictBasic";
  	
  	if(dictBasicType == 1){
		//新增
		 obj = {
	    	'code':code,
	    	'value':value,
	    	'display':display,
	    	'dictOrder':dictOrder,
	    	'isUsing':isUsing==1?false:true
	     };
  	}else{
  		//修改
  		var thisId = $("#bannerId").val();
  		obj = {
  			'code':code,
	    	'value':value,
	    	'display':display,
	    	'dictOrder':dictOrder,
	    	'isUsing':isUsing==1?false:true,
  			'id':thisId
  		};
  	}

	request({
		url:url,
		data:obj,
		type:"post",
		//ajaxform:true,
		callback:function(f) {
			 console.log(f);
			if(f.code=="rest.success"){
				if(dictBasicType == 1){
					ly.alert('保存成功',function(){	            
			    		ly.goto("dictBasic.html");
			        });
				}else{
					ly.alert('修改成功',function(){	           
			    		ly.goto("dictBasic.html");
			        });
				}
			}else{
				ly.toast(f.desc);
			}
		}
	});
  	
}

